<?php

namespace App\Console\Commands;

use Carbon\Carbon;
use App\Models\GoldRecord;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class RecallUnsettledRecord extends Command
{
	protected $signature = 'cc:recall-unsettled-record';

	protected $description = "Recall unsettled storm record";

	public function handle()
	{
		$id = $this->ask("Input GRID like: 1234");

		if (! $record = GoldRecord::find($id)) {
			$this->error('Expired record does not exist.');
			return;
		}

		DB::transaction(function () use ($record) {
			$data = ['gold_record_id' => $record->id];
			account_coins($record->user)->plusSystemTransferCoins($record->resell_price, $data);
			$record->update(['is_record_handled' => true, 'finished_at' => Carbon::now()]);
		});

		$this->info('Success.');
	}
}
